package com.ruoyi.web.controller.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysCustomer;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.CustomerService;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/customer/info")
public class CustomerController extends BaseController {

    @Autowired
    ISysPostService sysPostService;
    @Autowired
    CustomerService customerService;
    @Autowired
    HttpServletRequest request;

    @RequestMapping("/saleList")
    public AjaxResult getSaleList() {
        return AjaxResult.success(sysPostService.selectSaleINPost());
    }

    @PreAuthorize("@ss.hasPermi('customer:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCustomer customer) {
        List<SysCustomer> sysCustomerList = findWayToCustomerList(customer);
        return getDataTable(sysCustomerList);
    }

    private List<SysCustomer> findWayToCustomerList(SysCustomer customer) {
        String username = getUsername();
        List<SysCustomer> list;
        //管理员可以查看所有客户信息
        if ("admin".equals(username)) {
            startPage();
            list = customerService.selectCustomerList(customer);
        } else {
            //非管理员只能查看自己数据
            customer.setCreateBy(username);
            startPage();
            list = customerService.selectCustomerListByUserName(customer);
        }
        return list;
    }

    @Log(title = "客户数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('customer:info:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCustomer customer) {
        List<SysCustomer> list = findWayToCustomerList(customer);
        ExcelUtil<SysCustomer> util = new ExcelUtil<SysCustomer>(SysCustomer.class);
        util.exportExcel(response, list, "客户数据");
    }

    @Log(title = "导入客户", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('customer:info:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SysCustomer> util = new ExcelUtil<SysCustomer>(SysCustomer.class);
        List<SysCustomer> customerList = util.importExcel(file.getInputStream());
        String createBy = getUsername();
        customerService.importCustomer(customerList,createBy);
        return success("导入成功");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysCustomer> util = new ExcelUtil<SysCustomer>(SysCustomer.class);
        util.importTemplateExcel(response, "客户数据");
    }

    /**
     * 根据客户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:info:edit')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable Long customerId) {
        SysCustomer sysCustomer = customerService.selectCustomerById(customerId);
        return success(sysCustomer);
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('customer:info:edit')")
    @Log(title = "客户更新", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCustomer customer) {
        if (customerService.updateCustomer(customer)>0){
            return success("更新成功");
        }
        return error("更新失败");
    }

}


